<?php
/*
 * This class is used for as Model for Voucher reference table 
 */

class Application_Model_VoucherStats extends Application_Model_Base {

    protected $_name = 'vouchers';
    protected $_primaryKey = 'id';
    
    
    

    /**
     * Fetch an individual entry
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('v' => 'vouchers'))
                ->where('id = ?', $id);
        // see reasoning in fetchEntries() as to why we return only an array
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get Vouchers
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchVouchers($where = "", $limit = "", $offset = "0", $order = 'id', $sort = 'DESC') {
        $select = $this->select()
                ->from(array('v' => 'vouchers'));
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        
        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total vouchers
     * @return int
     */
    public function getVoucherCount($where = '') {
        $select = $this->select()
                ->from(array('v' => 'vouchers'), Array('cnt' => 'count(*)'));
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }
    
    
    /**
     *  This function is used to get total vouchers
     * @return int
     */
    public function getTotalVoucherEarning($where = '') {
        $select = $this->select()
                ->from(array('v' => 'vouchers'), Array('total' => 'sum(price)'));
        if ($where != '') {
            $select->where($where);
        }
        #echo $select; 
        #exit;
        $rows = parent::fetchRow($select);
        return($rows->total);
    }
    

}
